package com.youtellv1.controller;

import com.youtellv1.config.ResultData;
import com.youtellv1.entity.ChildPay;
import com.youtellv1.service.ChildPayService;
import com.youtellv1.service.ChildService;
import com.youtellv1.service.ChildSignService;
import lombok.var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.youtellv1.dto.ChildPayDTO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/v1/manager/childPay")
public class ChildPayController {
    @Autowired
    private ChildPayService childPayService;

    @Autowired
    private ChildService childService;

    @Autowired
    private ChildSignService childSignService;

    /**
     * 分页获取缴费列表（可选按 cid 和 month 过滤）
     */
    @GetMapping("/list")
    public ResultData<Map<String, Object>> list(@RequestParam Integer cid,
                                                @RequestParam(required = false) String month,
                                                @RequestParam(defaultValue = "1") int page,
                                                @RequestParam(defaultValue = "8") int size) {
        try {
            Map<String, Object> rawData = childPayService.getPayList(cid, month, page, size);

            List<ChildPay> list = (List<ChildPay>) rawData.get("list");
            List<ChildPayDTO> dtoList = new ArrayList<>();
            for (ChildPay cp : list) {
                ChildPayDTO dto = new ChildPayDTO();
                BeanUtils.copyProperties(cp, dto);
                var child = childService.findById(cp.getCid());
                if (child != null) {
                    dto.setCname(child.getCname());
                    dto.setCsex(child.getCsex());
                }
                dtoList.add(dto);
            }

            // 替换 list 为 dtoList
            rawData.put("list", dtoList);

            return new ResultData<>(rawData);
        } catch (Exception e) {
            return new ResultData<>(500, "分页获取失败: " + e.getMessage(), null);
        }
    }

    @GetMapping("/count")
    public ResultData<Integer> countSign(
            @RequestParam Integer cid,
            @RequestParam String start,
            @RequestParam String end) {
        try {
            int count = childSignService.countByCidAndDateRange(cid, start, end);
            return new ResultData<>(count);
        } catch (Exception e) {
            return new ResultData<>(500, "查询签到数量失败：" + e.getMessage(), null);
        }
    }

    @PostMapping("/add")
    public ResultData<ChildPayDTO> addChildPay(@RequestBody ChildPay childPay) {
        try {
            childPayService.saveService(childPay);

            var child = childService.findById(childPay.getCid());
            ChildPayDTO dto = new ChildPayDTO();
            BeanUtils.copyProperties(childPay, dto);
            if (child != null) {
                dto.setCname(child.getCname());
            }

            return new ResultData<>(dto);
        } catch (Exception e) {
            return new ResultData<>(500, "新增缴费失败：" + e.getMessage(), null);
        }
    }

    @GetMapping("/checkname")
    public ResultData<Map<String, Object>> checkName(@RequestParam("id") Integer cid) {
        try {
            var child = childService.findById(cid);
            if (child == null) {
                return new ResultData<>(404, "未找到该幼儿", null);
            }
            Map<String, Object> map = new HashMap<>();
            map.put("cid", child.getCid());
            map.put("cname", child.getCname());
            return new ResultData<>(map);
        } catch (Exception e) {
            return new ResultData<>(500, "查询失败：" + e.getMessage(), null);
        }
    }

    @PostMapping("/delete")
    public ResultData<Void> delete(@RequestBody ChildPay childPay) {
        try {
            childPayService.deleteService(childPay);
            return new ResultData<>(200, "删除成功", null);
        } catch (Exception e) {
            return new ResultData<>(500, "删除失败: " + e.getMessage(), null);
        }
    }

    @PostMapping("/update")
    public ResultData<Void> updateChildPay(@RequestBody ChildPay childPay) {
        try {
            if (childPay.getCpid() == null) {
                return new ResultData<>(400, "cpid不能为空", null);
            }
            childPayService.updateService(childPay);
            return new ResultData<>();
        } catch (Exception e) {
            return new ResultData<>(500, "修改失败 : " + e.getMessage(), null);
        }
    }

    @GetMapping("/detail")
    public ResultData<ChildPay> getDetail(@RequestParam("id") Integer id) {
        try {
            ChildPay childPay = childPayService.findById(id);
            if (childPay != null) {
                return new ResultData<>(childPay);
            } else {
                return new ResultData<>(404, "未找到缴费记录", null);
            }
        } catch (Exception e) {
            return new ResultData<>(500, "查询失败：" + e.getMessage(), null);
        }
    }
}
